/* $RCSFile: Shortcut.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/Shortcut/Shortcut.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:22  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:59  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Core;
using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Settings;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.Shortcut
{
    public class AStr
    {
        public const string LinkedModule     =   "SHORTCUT_LINKEDMODULE";
    }
    /// <summary>
    /// Initializer class for Shortcut 
    /// </summary>
    public class ShortcutSI : PortalModuleControlSI
    {
        protected ModulesDB mdb                 =   new ModulesDB();
        public ShortcutSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ShortcutSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ShortcutSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ShortcutSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                   =   pm.ExtendedResourcesGroup;

			// Get a list of modules of the current running portal
            DataTable tbMod                 =   mdb.GetModulesSinglePortal(portalSettings.PortalID);
            CustomListDataType ModList      =   new CustomListDataType(tbMod, "ModuleTitle", "ModuleID");
			SettingItemSpecial LinkedModule =   new SettingItemSpecial(AStr.LinkedModule,ModList);
			LinkedModule.Required           =   true;
			LinkedModule.Order              =   1;
			LinkedModule.Value              =   "0";
			LinkedModule.ExtendedGroupName  =    extgrp;
            LinkedModule.EnglishName        =   "Shortcut module";
            LinkedModule.Description        =   "Enter the module linked to this module";
			bSettings.AddSetting(LinkedModule); 

            if ( ModuleTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine("[C]ShortCut Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
    }

	/// <summary>
	/// Shortcut control provide a quick way to duplicate
	/// a module content in different page of the portal
	/// </summary>
	public partial class Shortcut : PortalModuleControl 
	{
        protected ModulesDB mdb                 =   new ModulesDB();


		#region General Implementaion
		public override Guid GuidID 
		{
			get
			{
				return new Guid("{23259716-3955-4f19-AB3F-503F99F63171}");
			}
		}
		#endregion

        public Shortcut()
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine("[C]ShortCut Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine("[C]ShortCut Ctor END.",ModuleTraceSwitch.Sw.Info);
		}

        public override string ExtendedResourcesGroup 
		{
            get
            {
       			const string extgrp             =   "SHORTCUT";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new ShortcutSI(bSettings,this);
        }

        protected virtual string ModuleKey
        {
            get
            {
                return AStr.LinkedModule;
            }
        }

		protected void Page_Load(object sender, System.EventArgs e) 
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ShortCut Page_Load BEG IsPostBack: {0}",IsPostBack),ModuleTraceSwitch.Sw.Info);
			/*
            if(!IsPostBack)
			{
			*/
			string ControlPath      =   string.Empty;

			//Try to get info on linked control
			int LinkedModuleID      =   Settings[ModuleKey].ToInt();
			SqlDataReader dr        =   null;
            try
            {
                dr                  =   mdb.GetModuleDefinitionByID(LinkedModuleID);
                if (dr.Read())
                    ControlPath     =   AddonNice.Settings.Path.ApplicationRoot + "/" + dr["DesktopSrc"].ToString();
            }
            catch (Exception ex)
            {
                if ( ModuleTraceSwitch.Sw.TraceError )
				    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ShortCut Page_Load ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
			finally
			{
                if ( dr != null )
				    dr.Close();
			}

			//Load control
			PortalModuleControl portalModule;
			try
			{
				if ( string.IsNullOrEmpty(ControlPath) )
				{
					PlaceHolderModule.Controls.Add(new LiteralControl("Module '" + LinkedModuleID + "' not found!  Use Admin panel to add a linked control."));
					return;
				}
				ModuleConfiguration  m          =   new ModuleConfiguration();           
				m.ModuleID                      =   LinkedModuleID;
				m.PageID                        =   ModuleBaseConfiguration.PageID;
				m.PaneName                      =   ModuleBaseConfiguration.PaneName;
				m.ModuleTitle                   =   ModuleBaseConfiguration.ModuleTitle;
				m.AuthorizedEditRoles           =   string.Empty;		//Readonly
				m.AuthorizedViewRoles           =   string.Empty;		//Readonly
				m.AuthorizedAddRoles            =   string.Empty;		//Readonly
				m.AuthorizedDeleteRoles         =   string.Empty;	//Readonly
				m.AuthorizedPropertiesRoles     =   ModuleBaseConfiguration.AuthorizedPropertiesRoles;
				m.CacheTime                     =   ModuleBaseConfiguration.CacheTime;
				m.ModuleOrder                   =   ModuleBaseConfiguration.ModuleOrder;
				m.DesktopSrc                    =   ControlPath;
                m.SupportCollapsable            =   ModuleBaseConfiguration.SupportCollapsable;
                m.ModuleSettingsID              =   ModuleBaseConfiguration.ModuleSettingsID;

                portalModule                    =   ADNPage.LoadPortalModule(ControlPath,portalSettings.PortalID,m,false);

				portalModule.Settings[StStr.ModuleSettingsApplyModuleLayout]    =   Settings[StStr.ModuleSettingsApplyModuleLayout];
				portalModule.Settings[StStr.ModuleSettingsModuleLayout]         =   Settings[StStr.ModuleSettingsModuleLayout];

				// added so ShowTitle is independent of the Linked Module
				portalModule.Settings[StStr.ModuleSettingsShowTitle]            =   Settings[StStr.ModuleSettingsShowTitle];

			    portalModule.PropertiesUrl              =   HttpUrlBuilder.BuildUrl(Config.ModulesPath + "/" + Config.ModulePropertyPage, PageID, ModuleID);
			    portalModule.AddUrl                     =   string.Empty;       //Readonly
			    portalModule.AddText                    =   string.Empty;      //Readonly
			    portalModule.EditUrl                    =   string.Empty;      //Readonly
			    portalModule.EditText                   =   string.Empty;     //Readonly
			    portalModule.OriginalModuleID           =   ModuleID;
                
                // added so that shortcut works for module "print this..." feature
				PlaceHolderModule.ID                    =   "Shortcut";

				//Add control to the page
				PlaceHolderModule.Controls.Add(portalModule);
			}
			catch(Exception ex)
			{
                if ( ModuleTraceSwitch.Sw.TraceError )
				    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ShortCut Page_Load Unable to load control {0}, ex: {1}",ControlPath,ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
            

            if ( ModuleTraceSwitch.Sw.TraceVerbose )
			    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ShortCut Page_Load END IsPostBack: {0}",IsPostBack),ModuleTraceSwitch.Sw.Info);
		}
	}
}
